package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.dao.TeacherDao;
import cn.edu.sdjzu.xg.bysj.dao.UserDao;
import cn.edu.sdjzu.xg.bysj.domain.*;
import exception.BysjException;

import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService {
    private static TeacherDao teacherDao = TeacherDao.getInstance();
    private static TeacherService teacherService = new TeacherService();

    private TeacherService() {
    }

    public static TeacherService getInstance() {
        return teacherService;
    }

    public int countAll(Department department) throws SQLException {

        return teacherDao.countAll(department);
    }

    public Collection<Teacher> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = teacherDao.findAll(conditionList, pagination, connection);
        JdbcHelper.close(connection);
        return teachers;
    }

    public Teacher find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //查询新添加的teacher元组，并形成Teacher对象
        Teacher findOfTeacher = teacherDao.find(id, connection);
        //查询新增加的User对象，形成User对象
        //User findOfUser = UserService.getInstance().find(id);
        //为Teacher对象的user字段赋值
        //findOfTeacher.setUser(findOfUser);
        JdbcHelper.close(connection);
        return findOfTeacher;
    }

    public boolean update(Teacher teacher, Connection connection) throws SQLException {
        Connection connection1 = connection;
        if (connection1 == null) {
            connection1 = JdbcHelper.getConn();
            boolean updated = teacherDao.update(teacher, connection1);
            return updated;
        } else {
            return teacherDao.update(teacher, connection);
        }
    }

    public Boolean add(Teacher teacher) throws SQLException, BysjException {
        //获得进行事务的连接，添加教师和添加用户都使用本连接对象
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false，开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            //添加actor对象，返回值为自动生成的id
            int actorId = ActorDao.getInstance().add(connection);
            teacher.setId(actorId);
            teacherDao.add(teacher, connection);
            //创建用户对象，密码和用户名取自teacher字段值no
            User user = new User(teacher.getNo(), teacher.getNo(), null, teacher);
            //增加用户
            added = UserDao.getInstance().add(user, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
            throw new BysjException("失败");
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = teacherDao.delete(id, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = teacherDao.delete(teacher, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}
